﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using BarCode.Common;
using BarCode.Bean;
using System.Globalization;

namespace BarCode.Module
{
    public class IniFileHelper
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(IniFileHelper));

        public static string path;             //INI文件名  

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
                    string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def,
                    StringBuilder retVal, int size, string filePath);

        //声明读写INI文件的API函数  
        public static void IniFile()
        {
            path = AppDomain.CurrentDomain.BaseDirectory + Consts.INI_FILE_NAME;
        }

        //类的构造函数，传递INI文件名  
        public static void WriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }

        //读INI文件  
        public static string ReadValue(string Section, string Key)
        {
            try
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
                return temp.ToString();
            }
            catch (Exception ex)
            {
                LogUtil.Error("读取INI失败", ex);
                return "";
            }
        }

        // 读取配置文件值  
        public static bool getBoolValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            if ("1".Equals(temp.ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string getStringValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }

        public static bool setStringValue(string Section, string Key, string value)
        {
           long ret =  WritePrivateProfileString(Section, Key, value, path);
           if (ret > 0)
           {
               return true;
           }
           else
           {
               return false;
           }
        }


        public static int getIntegerValue(string Section, string Key)
        {
            try
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
                return Convert.ToInt32(temp.ToString());
            }
            catch (Exception ex)
            {
                LogUtil.Error("读取整型数据失败", ex);
                return -1;
            }
        }

        public static int getIntegerValue(string Section, string Key, int defaultVal)
        {
            try
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
                return Convert.ToInt32(temp.ToString());
            }
            catch (Exception ex)
            {
                LogUtil.Error("读取整型数据失败", ex);
                return defaultVal;
            }
        }

        // 读取时间差
        public static int getDiffHour()
        {
            string diffHour = ReadValue("MySql", "DIFF_HOUR");
            if (String.IsNullOrEmpty(diffHour))
            {
                LogUtil.Error("数据库系统时间差未设置。");
                return 0;
            }
            else
            {
                try
                {
                    int ret = Convert.ToInt16(diffHour);
                    return ret;
                }
                catch (Exception ex)
                {
                    LogUtil.Error("数据库系统时间差设置不正确：" + diffHour);
                    return 0;
                }

            }
        }

        // 读取时间差
        public static string getWorkStartTime()
        {
            string strStart = ReadValue("System", "WORK_START_TIME");
            if (String.IsNullOrEmpty(strStart))
            {
                LogUtil.Error("工作开始时间未设置。");
                return "0000";
            }
            else
            {
                try
                {
                    string dtStr = DateTime.Now.ToString("yyyyMMdd") + strStart + "00";
                    DateTime.ParseExact(dtStr, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
                    return strStart;
                }
                catch (Exception ex)
                {
                    LogUtil.Error("工作开始时间设置不正确：" + strStart);
                    return "0000";
                }

            }
        }

        /// <summary>
        /// 读取Mysql数据库访问信息
        /// </summary>
        /// <returns></returns>
        public static MySqlAccessInfo ReadMySqlAccessInfo(String section)
        {

            try
            {
                MySqlAccessInfo info = new MySqlAccessInfo();
                info.USERID = DESUtil.Decrypt(ReadValue(section, "USER_ID"));
                info.USERPWD = DESUtil.Decrypt(ReadValue(section, "USER_PWD"));
                return info;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
                return null;
            }

        }

        /// <summary>
        /// 获取机芯机壳的扫码顺序设定
        /// </summary>
        /// <returns></returns>
        public static String ReadJxJkOrder()
        {
            return ReadValue("System", "JXJKORDER");
        }


        /// <summary>
        /// 获取机芯机壳的扫码顺序设定
        /// </summary>
        /// <returns></returns>
        public static void ReadLoadServerDataFlag()
        {
            GlobalVal.LoadUsers = ReadValue("System", "LoadUsers");
            GlobalVal.LoadLicence = ReadValue("System", "LoadLicence");
            GlobalVal.LoadMnoprnPre = ReadValue("System", "LoadMnoprnPre");
            GlobalVal.LoadZjmfPre = ReadValue("System", "LoadZjmfPre");
            GlobalVal.LoadCustomer = ReadValue("System", "LoadCustomer");
        }

    }
}
